/**
 *
 */
package anysware.server;

import java.util.HashMap;
import java.util.Map;

import org.lpny.groovyrestlet.GroovyRestlet;
import org.restlet.Component;
import org.restlet.data.Protocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * TODO: Doc me
 *
 * @author keke <keke@codehaus.org>
 * @version
 * @revision $Revision:$
 */
public class Launcher {
	private static final Logger	LOG	= LoggerFactory.getLogger(Launcher.class);

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(final String[] args) throws Exception {
		final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
		        args[0]);
		ctx.registerShutdownHook();
		final GroovyRestlet gr = new GroovyRestlet(ctx);
		final Map<String, Object> context = new HashMap<String, Object>();
		context.put("gwtRoot", "/anysware");
		context.put("apiRoot", "/anysware/api");
		final Component comp = (Component) gr.build(context, Thread
		        .currentThread().getContextClassLoader().getResource(
		                "anysware.groovy").toURI());
		Runtime.getRuntime().addShutdownHook(new Thread() {

			@Override
			public void run() {
				try {
					comp.stop();
					if (LOG.isInfoEnabled()) {
						LOG.info("Server stopped");
					}
				} catch (final Exception e) {
					if (LOG.isErrorEnabled()) {
						LOG.error("Failed to stop server", e);
					}
				}
			}
		});
		comp.getServers().add(Protocol.HTTP, 8080);
		comp.start();
		if (LOG.isInfoEnabled()) {
			LOG.info("Server started");
		}
	}

}
